import { deleteAxios, get, post } from "@/service/axiosManager";
import { message } from "antd";
import { PageInfo } from "../general/modal/pageModal";
import { CreateApplicationModal, DatasetData, RequestDatasetData, RequestKnowledgeBase, CreateThirdApplication, RequestExecTool } from "./modal/requestModal";

export const sendTestMsg = async ({msg, appId}: {msg: string, appId?: string}) => {
    try {
        const param = {
            "question": msg,
            "appId": appId
        }
        const response = await post("ai/chat/qa", param);
        return response;
    } catch (error) {
        const msg = "发送消息失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const createKnowledgeBase = async (data: RequestKnowledgeBase) => {
    try {
        const response = await post('/dataset/create', data, true);
        return response;
    } catch (error) {
        message.error('创建知识库失败');
        console.error('创建知识库失败:', error);
    }
}

export const updateKnowledgeBase = async (data: RequestKnowledgeBase) => {
    try {
        const response = await post('/dataset/update', data, true);
        return response;
    } catch (error) {
        message.error('修改知识库失败');
        console.error('修改知识库失败:', error);
    }
}

export const deleteKnowledgeBase = async (id: string) => {
    try {
        const response = await post('/dataset/delete', {id}, true);
        return response;
    } catch (error) {
        message.error('删除知识库失败');
        console.error('删除知识库失败:', error);
    }
}

export const getKnowledgeBaseList = async () => {
    try {
        const response = await post('/dataset/list', {}, true);
        return response;
    } catch (error) {
        message.error('获取知识库列表失败');
        console.error('获取知识库列表失败:', error);
    }
}

export const isBindKnowledgeBase = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id
        }
        const response = await get("/dataset/isBind", param, true);
        return response;
    } catch (error) {
        const msg = "获取知识库引用验证失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getDatasetList = async ({page, id, searchText = ""}:{page?:PageInfo, id: string, searchText?:string}) => {
    try {
        if (!page) page = new PageInfo({});
        const param = {
            "pageNum": page.getPageNum(),
            "pageSize": page.getPageSize(),
            "params": {
                "datasetId": id,
                "searchText": searchText
            }
        }
        const response = await post("dataset/collection/list", param, true);
        return response;
    } catch (error) {
        message.error('获取数据集列表失败');
        console.log('获取数据集列表失败:',error);
    }
}

export const detailDataset = async ({id}: {id: string}) => {
    try {
        const parma = {
            "id": id
        }
        const response = await get("dataset/collection/detail", parma, true);
        return response
    } catch(error) {
        message.error('获取数据集详情失败');
        console.log('获取数据集详情失败:',error);
    }
}

export const updateDatasetName = async ({id, newName}: {id: string, newName: string}) => {
    try {
        if (!id || id.length <= 0) throw new Error("id不能为空");
        const param = {
            "id": id,
            "name": newName
        }
        const response = await post("dataset/collection/update",param, true);
        return response
    } catch (error) {
        message.error("修改集合信息失败");
        console.log("修改集合信息失败", error);
    }
}

export const deleteDataset = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id
        }
        const response = await deleteAxios("dataset/collection/delete", param, true);
        return response;
    } catch (error) {
        message.error("删除集合失败");
        console.log("删除集合失败", error);
    }
}

export const getDatasetModels = async () => {
    try {
        const param = {};
        const response = await get("dataset/models", param, true);
        return response
    } catch (error) {
        message.error("获取模型名称失败");
        console.log("获取模型名称失败", error);
    }
}

export const createEmptyDataset = async ({datasetId, name, type = "virtual"}: {datasetId: string, name: string, type?: string}) => {
    try {
        const param = {
            "datasetId": datasetId,
            "name": name,
            "type": type
        }
        const response = await post("dataset/collection/create", param, true);
        return response
    } catch (error) {
        message.error("创建空集合失败");
        console.log("创建空集合失败", error);
    }
}

export const createTextDataset = async ({datasetId, name, trainingType, text}: {datasetId: string, name: string, trainingType: string, text: string}) => {
    try {
        const param = {
            "datasetId": datasetId,
            "name": name,
            "trainingType": trainingType,
            "text": text
        }
        const response = await post("dataset/collection/create/text", param, true);
        return response;
    } catch (error) {
        message.error("创建文本集合失败");
        console.log("创建文本集合失败", error);
    }
}

export const createLinkDataset = async ({datasetId, trainingType, link}: {datasetId: string, trainingType: string, link: string}) => {
    try {
        const param = {
            "datasetId": datasetId,
            "trainingType": trainingType,
            "link": link
        }
        const response = await post("dataset/collection/create/link", param, true);
        return response;
    } catch (error) {
        message.error("创建链接集合失败");
        console.log("创建链接集合失败", error);
    }
}

export const createFileDataset = async ({datasetId, trainingType, file}: {datasetId: string, trainingType: string, file: File}) => {
    try {
        const param = new FormData();
        param.append("file", file);
        param.append("data", `{"datasetId": "${datasetId}","trainingType": "${trainingType}"}`);
        // const param = {
        //     "file": file,
        //     "data": {
        //         "datasetId": datasetId,
        //         "trainingType": trainingType
        //     }
        // }
        const response = await post("dataset/collection/create/localFile", param, true);
        return response;
    } catch (error) {
        message.error("创建链接集合失败");
        console.log("创建链接集合失败", error);
    }
}

export const getDataList = async ({page, id, searchText = ""}: {page?: PageInfo, id: string, searchText: string}) => {
    try {
        console.log("page: \n", page);
        if (!page) page = new PageInfo({});
        const param = {
            "pageSize": page.getPageSize(),
            "offset": page.getPageSize() * (page.getPageNum() - 1),
            "params": {
                "collectionId": id,
                "searchText": searchText
            }
        };
        const response = await post("dataset/data/list", param, true);
        return response;
    } catch (error) {
        const msg = "获取集合数据列表失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getDataDetail = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id,
        }
        const response = await get("dataset/data/detail", param, true);
        return response;
    } catch (error) {
        const msg = "获取集合数据详情失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const createDatasetData = async ({param}: {param: RequestDatasetData}) => {
    try {
        const response = await post("dataset/data/create", param, true);
        return response;
    } catch (error) {
        const msg = "添加集合数据失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const updateDatasetData = async ({param}: {param: DatasetData}) => {
    try {
        const response = await post("dataset/data/update", param, true);
        return response;
    } catch (error) {
        const msg = "修改集合数据失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const deleteDatasetData = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id
        }
        const response = await deleteAxios("dataset/data/delete", param, true);
        return response;
    } catch (error) {
        const msg = "删除集合数据失败";
        message.error(msg);
        console.log(msg, error);
    } 
}

export const createApplication = async ({param}: {param: CreateApplicationModal}) => {
    try {
        const response = post("application/create", param, true);
        return response;
    } catch (error) {
        message.error("创建应用失败");
        console.log("创建应用失败", error);
    }
}

export const updateApplication = async ({param}: {param: CreateApplicationModal}) => {
    try {
        const response = post("application/update", param, true);
        return response;
    } catch (error) {
        message.error("修改应用失败");
        console.log("修改应用失败", error);
    }
}

export const deleteApplication = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id
        }
        const response = deleteAxios("application/delete", param, true);
        return response;
    } catch (error) {
        message.error("删除应用失败");
        console.log("删除应用失败", error);
    }
}

export const getApplicationList = async () => {
    try {
        const response = await post("application/list");
        return response
    } catch (error) {
        message.error("获取应用列表失败");
        console.log("获取应用列表失败", error);
    }
}

export const getApplicationListLocal = async () => {
    try {
        const response = await post("application/list/local");
        return response
    } catch (error) {
        const msg = "获取本地应用失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getApplicationModels = async () => {
    try {
        const response = await get("application/models");
        return response
    } catch (error) {
        message.error("获取模型名称失败");
        console.log("获取模型名称失败", error);
    }
}

export const getThirdApplicationType = async () => {
    try {
        const resposne = await get("application/third/type");
        return resposne;
    } catch (error) {
        const msg = "获取第三方应用类型失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getThirdApplicationList = async (searchText: string = "") => {
    try {
        let param = {};
        if (searchText && searchText.length > 0) {
            param = {
                "name": searchText
            }
        }
        const response = await post("application/list/third", param);
        return response;
    } catch (error) {
        const msg = "获取第三方应用失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const createThirdApplication = async ({param}: {param: CreateThirdApplication}) => {
    try {
        const response = await post("application/create/third", param);
        return response;
    } catch (error) {
        const msg = "创建第三方应用失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const updateThirdApplication = async ({param}: {param: CreateThirdApplication}) => {
    try {
        const response = await post("application/update/third", param);
        return response;
    } catch (error) {
        const msg = "修改第三方应用失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const deleteThirdApplication = async ({id}: {id: string}) => {
    try {
        const param = {
            "id": id
        }
        const resposne = await deleteAxios("application/delete/third", param);
        return resposne;
    } catch (error) {
        const msg = "删除第三方应用失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getChatSession = async ({id}: {id: string}) => {
    try {
        const param = {
            appId: id
        };
        const response = await get("ai/chat/session", param, true);
        return response
    } catch (error) {
        message.error("获取应用会话失败");
        console.log("获取应用会话失败", error);
    }
}

export const getToolsList = async () => {
    try {
        const response = await get("tools/list");
        return response;
    } catch(error) {
        const msg = "获取工具箱列表失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getToolDetail = async ({id}: {id: string}) => {
    try {
        const param = {
            appId: id
        }
        const response = await get("tools/detail", param);
        return response;
    } catch(error) {
        const msg = "获取工具详情失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const saveToolPrompt = async ({id, prompt}: {id: string, prompt: string}) => {
    try {
        const param = {
            appId: id,
            prompt: prompt
        }
        const response = await post("tools/save", param);
        return response;
    } catch(error) {
        const msg = "保存工具提示词失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const resetToolPrompt = async ({id}: {id: string}) => {
    try {
        const param = {
            appId: id
        }
        const response = await post("tools/restore", param);
        return response;
    } catch(error) {
        const msg = "重置工具提示词失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const execTool = async ({param}: {param: RequestExecTool}) => {
    try {
        const response = await post("tools/run", param);
        return response;
    } catch(error) {
        const msg = "执行工具失败";
        message.error(msg);
        console.log(msg, error);
    }
}

export const getMcpConfigs = async () => {
    try {
        const resposne = await post("keyword/mcp/config");
        return resposne;
    } catch(error) {
        const msg = "获取mcp工作流列表失败";
        message.error(msg);
        console.log(msg, error);
    }
}